#!/usr/bin/env python
#coding: utf-8
# 
# $Id$
# 

import socket
import sys
import array    # C interface
import time
import struct

sys.path.append('..')
from slib.simp import *

if __name__ == '__main__':
    if (len(sys.argv) < 2):
        print 'recver.py <port>'
        port = 8002
    else:
        port = int(sys.argv[1])

 
    s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  

    s.settimeout(5) 
    s.bind(('', port))  
    print "waiting... @", port
    old_a = 1
    while 1:  
        try:  
            s.settimeout(5)  
            d,a=s.recvfrom(8192)  
            if (old_a != a):
                print 'from', a
                old_a = a

            arr = array.array('B', d)
            (data, length) = arr.buffer_info()

            # src_type,src_id,dst_type,dst_id,serial,pkg_num,pkg_lenl,h,id(CMD_DATA),param,lenl,lenh
            len = arr[I_SERVICE_LEN]*256+arr[I_SERVICE_LEN2]

            if arr[I_SERVICE_CMD] == CMD_DEBUG:
                print arr[I_SERVICE_DATA:], 
                #print d[I_SERVICE_DATA:]
            elif arr[I_SERVICE_CMD] == CMD_DATA:
                print "NR ", len, arr[I_SERVICE_CMD:]
        except socket.timeout:  
            print ".",
